package com.zhiwei.controller;

import com.zhiwei.service.FileService;
import com.zhiwei.vo.ImagVO;
import com.zhiwei.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;
    /**
     *
     * @param file
     * @return
     */
//    @PostMapping("/upload")
//    public SysResult upload(MultipartFile file) throws IOException {
//        String filename = file.getOriginalFilename();
//        String fileDir = "D:/Maven_Proj/JT-imags";
//        File dirFile = new File(fileDir);
//        if (!dirFile.exists()){
//            dirFile.mkdirs();
//        }
//        String filePath = fileDir + "/" + filename;
//        file.transferTo(new File(filePath));
//        return SysResult.success();
//    }
    @PostMapping("/upload")
    public SysResult upload(MultipartFile file) throws IOException {
        ImagVO imagVO = fileService.upload(file);
        if (imagVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imagVO);
    }
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        fileService.deleteFile(virtualPath);
        return SysResult.success();
    }
}
